package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.entity.User;
import com.example.demo.entity.UserAddress;
import com.example.demo.mapper.AddressMapper;
import com.example.demo.service.UserAddressService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Resource
    private AddressMapper addressMapper;
    public Integer addAddress(UserAddress userAddress){
        return addressMapper.insert(userAddress);
    }

    public Integer deleteAddress(Integer addressId, String openId){
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_address_id",addressId);
        queryWrapper.eq("open_id",openId);
        return addressMapper.delete(queryWrapper);
    }

    public UserAddress getAddressById(Integer addressId, String openId){
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_address_id",addressId);
        queryWrapper.eq("open_id",openId);
        UserAddress userAddress = addressMapper.selectOne(queryWrapper);
        return userAddress;
    }

    public Integer editAddress(UserAddress userAddress){
        UpdateWrapper<UserAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_address_id",userAddress.getUserAddressId());
        return addressMapper.update(userAddress,updateWrapper);
    }

    public List<UserAddress> getAddressList(String openId){
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id",openId);
        List<UserAddress> res = addressMapper.selectList(queryWrapper);
        return res;
    }

}
